package com.syezon.wifikey.ad.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class AppInstallReceiver extends BroadcastReceiver {

    public static final String TAG = AppInstallReceiver.class.getSimpleName();

    private static List<AppInstallObserver> appInstallObservers = new ArrayList<>();

    private static AppInstallReceiver appInstallReceiver;

    public static BroadcastReceiver getBroadcastReceiver() {
        if (appInstallReceiver == null) {
            synchronized (AppInstallReceiver.class) {
                if (appInstallReceiver == null) {
                    appInstallReceiver = new AppInstallReceiver();
                }
            }
        }
        return appInstallReceiver;
    }

    public static void registerAppInstallReceiver(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        context.getApplicationContext().registerReceiver(getBroadcastReceiver(), filter);
    }

    public static void unRegisterAppInstallReceiver(Context context) {
        if (appInstallReceiver != null) {
            try {
                context.unregisterReceiver(getBroadcastReceiver());
            } catch (Exception e) {
            }
        }
    }

    public static void registerObserver(AppInstallObserver observer) {
        if (observer != null && !appInstallObservers.contains(observer)) {
            appInstallObservers.add(observer);
        }
    }

    public static void unRegisterObserver(AppInstallObserver observer) {
        if (appInstallObservers.contains(observer)) {
            appInstallObservers.remove(observer);
        }
    }

    /**
     * @param pkgName 通知应用添加事件
     */
    private static void notifyAppAdded(String pkgName) {
        if (!appInstallObservers.isEmpty()) {
            int size = appInstallObservers.size();
            for (int i = 0; i < size; i++) {
                AppInstallObserver appInstallObserver = appInstallObservers.get(i);
                if (appInstallObserver != null) {
                    appInstallObserver.added(pkgName);
                }
            }
        }
    }

    /**
     * @param pkgName 通知应用替换事件
     */
    private static void notifyAppReplaced(String pkgName) {
        if (!appInstallObservers.isEmpty()) {
            int size = appInstallObservers.size();
            for (int i = 0; i < size; i++) {
                AppInstallObserver appInstallObserver = appInstallObservers.get(i);
                if (appInstallObserver != null) {
                    appInstallObserver.replaced(pkgName);
                }
            }
        }
    }

    /**
     * @param pkgName 通知删除应用事件
     */
    private static void notifyAppRemoved(String pkgName) {
        if (!appInstallObservers.isEmpty()) {
            int size = appInstallObservers.size();
            for (int i = 0; i < size; i++) {
                AppInstallObserver appInstallObserver = appInstallObservers.get(i);
                if (appInstallObserver != null) {
                    appInstallObserver.removed(pkgName);
                }
            }
        }
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        final String pkgName = intent.getData().getSchemeSpecificPart();
        final String action = intent.getAction();
        if (!TextUtils.isEmpty(pkgName) && !TextUtils.isEmpty(action)) {
            if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
                notifyAppAdded(pkgName);
                //获取设备上的应用并保存到文件
            } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
                notifyAppRemoved(pkgName);
            } else if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
                notifyAppReplaced(pkgName);
            }
        }

    }
}